﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
//6.MAXSIZE为10MB大小；
#define MAXSIZE 1024*1024*10

//main162：大文件的拷贝
int main162(int argc, char* argv[])
{
	unsigned int start_time = time(NULL);
	if (argc < 3)
	{
		//2.比如三个参数：argv[0],argv[1],argv[2]分别为MyCpCharInputBug    video.mp4    vide.2.mp4； 
		printf("1.自定义的cp函数，参数必须多于或等于3个！即：缺少参数！\n");
		return -1;
	}

	FILE* fp1 = fopen(argv[1], "rb");
	FILE* fp2 = fopen(argv[2], "wb");
	if (!fp1 || !fp2)
	{
		printf("3.操作文件失败！\n");
		return -2;
	}

	//7.获取文件属性，用来求出文件的字节数；这次用结构指针stat*，会方便一些，不用加&取地址符号，指针就是地址；
	//struct stat s;
	struct stat* s = NULL;
	//8.argv[1]，指源文件的地址，即../vide.mp4；
	//stat(argv[1], &s);
	stat(argv[1], s);
	int size = 0;
	char* ch;
	if (s->st_size < MAXSIZE)
	{
		size = s->st_size;
		ch = (char*)malloc(sizeof(char) * s->st_size);
	}
	else
	{
		size = MAXSIZE;
		ch = (char*)malloc(sizeof(char) * MAXSIZE);//10MB
	}
	while (!feof(fp1))
	{
		memset(ch, 0, size);
		//5.fread读取块之后，会有一个返回值，就是读取到的块数。fwrite按照这个返回值len，进行写入；
		int len = fread(ch, 1, size, fp1);//123
		fwrite(ch, len, 1, fp2);
	}
	fclose(fp1);
	fclose(fp2);
	free(ch);

	unsigned int end_time = time(NULL);

	printf("4.拷贝文件总花费时间为：%d(s)\n", end_time - start_time);

	system("pause");
	return EXIT_SUCCESS;
}